package com.proj.hot.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.TextView;

import com.proj.hot.GlobalParams;
import com.proj.hot.R;
import com.proj.hot.adapter.RecordGridAdapter;
import com.proj.hot.entity.SeeRecordBean;
import com.proj.hot.utils.DebugUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 * 观看记录
 */
public class SeeRecordActivity extends BaseActivity {

    private GridView gridView;
    private RecordGridAdapter recordGridAdapter;
    private List<SeeRecordBean> listData;

    private String accountid = "";

    private List<JSONObject> jsList = new ArrayList<JSONObject>();
    private JSONArray jsonArray = new JSONArray();
    private JSONObject jsonObject;

    private TextView seerecod_nosee;

    private TextView seer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_seerecord);
        seer = (TextView)this.findViewById(R.id.seer);
        if(GlobalParams.screenHeight == 1080){
            seer.setTextSize(25);

        }
        seer.setText("<  "+"观看记录");
        seerecod_nosee = (TextView)this.findViewById(R.id.seerecod_nosee);
        gridView = (GridView) this.findViewById(R.id.seerecod_grid);
        recordGridAdapter = new RecordGridAdapter(this, jsList);
        gridView.setAdapter(recordGridAdapter);
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(SeeRecordActivity.this, MovieDetailActivity.class);
                try {
                    // DebugUtil.i("zzzg", "点击事件,i=" + i + "recode=" + jsList.get(i).getString("recode"));
                    intent.putExtra("contentid", jsList.get(i).getString("contentid"));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                startActivity(intent);
            }
        });
    }

    private void getSeeRecordData() {

        try {
            /**
             * ---------------body------------------
             */
            JSONObject body = new JSONObject();
            body.put("accountid", GlobalParams.user_id);
            DebugUtil.i("SeeRecordActivity", "accountid=" + GlobalParams.user_id);

            RequestParams params = new RequestParams(GlobalParams.reqHost + "account/getPlayRecord");
            params.setAsJsonContent(true);
            params.setBodyContent(body.toString().trim());

            /**
             * ---------------send------------------
             */
            x.http().post(params, new Callback.CommonCallback<String>() {
                @Override
                public void onSuccess(String s) {
                    try {
                        DebugUtil.i("SeeRecordActivity", "观看记录返回数据=" + s);
                        JSONObject jData = new JSONObject(s);
                        jsonObject = jData.getJSONObject("respbody");
                        jsonArray = jsonObject.getJSONArray("data");
                        jsList.clear();
                        for (int i = 0; i < jsonArray.length(); i++) {
                            jsList.add(jsonArray.getJSONObject(i));
                        }
                        DebugUtil.i("SeeRecordActivity", "观看记录个数=" + jsList.size());
                        if(jsList.size()==0){
                            gridView.setVisibility(View.GONE);
                            seerecod_nosee.setVisibility(View.VISIBLE);
                        }

                        recordGridAdapter.notifyDataSetChanged();

                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                }

                @Override
                public void onFinished() {

                }

                @Override
                public void onCancelled(CancelledException e) {

                }

                @Override
                public void onError(Throwable throwable, boolean b) {
                    DebugUtil.toast(SeeRecordActivity.this,"观看记录服务异常，请稍后重试!");
                }
            });


        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @Override
    protected void onResume() {
        super.onResume();
        getSeeRecordData();
    }
}
